<?php

namespace addons\member\user\controller;

class MemberClassSet extends \web\user\controller\AddonUserBase{
    
    public function index(){
        $typeM = new \addons\member\model\LevelConfig();
        $level_list = $typeM->getDataList(-1,-1,'','id,title,level','id asc');
        $this->assign('level_list',$level_list);
        return $this->fetch();
    }
    
    public function loadList(){
        $keyword = $this->_get('keyword');
        $is_auth = $this->_get('is_auth');
        $level = $this->_get('level');
        $is_frozen = $this->_get('is_frozen');
        $startDate = $this->_get('startDate');
        $endDate = $this->_get("endDate");
        $filter = ' a.logic_delete=0 and a.is_actor = 1 ';
        if ($startDate != null && $endDate != null)
            $filter .= ' and (a.register_time >= \'' . $startDate . ' 00:00:00\' and a.register_time <= \'' . $endDate . ' 23:59:59\')';
        elseif ($startDate != null)
            $filter .= ' and (a.register_time >= \'' . $startDate . ' 00:00:00\')';
        elseif ($endDate != null)
            $filter .= ' and (a.register_time <= \'' . $endDate . ' 23:59:59\')';

        if ($keyword != null) {
            // $filter .= ' and (a.username like \'%' . $keyword . '%\')';
            $filter .= ' and (a.username like \'%' . $keyword . '%\' or a.phone  like \'%' . $keyword . '%\' )';
        }

        if ($is_auth != null) {
            $filter .= ' and a.is_auth = ' . $is_auth;
        }
        if ($level != null) {
            $filter .= ' and a.level = ' . $level;
        }
        if ($is_frozen != null) {
            $filter .= ' and a.is_frozen = ' . $is_frozen;
        }

        $m = new \addons\member\model\MemberAccountModel();
        $total = $m->getMemberTotal($filter);
        $order = 'a.id desc';
        $fileds = 'a.*';
        $rows = $m->getMemberList($this->getPageIndex(), $this->getPageSize(), $filter, $fileds, $order);
        $list = (new \addons\member\model\MemberClass)->where('is_show=1')->order('order_index desc')->column('id,class_title');
        foreach ($rows as $key => &$v) {
            $v['class_name'] = isset($list[$v['class_id']])?$list[$v['class_id']]:'';
        }
        return $this->toDataGrid($total, $rows);
    }
    
    public function edit(){
        $m = new \addons\member\model\MemberAccountModel();
        if(IS_POST){
            $data = $_POST;
            if($data['id']){
                $m->save($data);
                return $this->successData();
            }else{
                return $this->failData('用户id为空');
            }
        }else{
            $list = (new \addons\member\model\MemberClass)->field('id,class_title')->where('is_show=1')->order('order_index desc')->select();
            $this->assign('class_list', $list);
            $this->assign('id', $this->_get('id'));
            $this->setLoadDataAction('loadData');
            return $this->fetch();
        }
    }
    
    public function loadData() {
        $id = $this->_get('id');
        $m = new \addons\member\model\MemberAccountModel();
        $data = $m->getDetail($id,'username,head_img,intro,id,class_id');
        return $data;
    }

    public function actor_set() {
        $m = new \addons\member\model\MemberAccountModel;
        if (IS_POST) {
            $pid = $this->_post('pid');
            if($pid < 1) return $this->failData('请选择正确的用户');
            $res = $m->where(['id'=>$pid,'is_actor'=>0])->update(['is_actor'=>1,'update_time'=>NOW_DATETIME]);
            if(!$res) return $this->failData('设置成为演员失败');
            return $this->successData();
        } else {
            $list = $m->field('id,username')->where(['logic_delete' => 0,'is_actor'=>0])->order('id asc')->limit(0,100)->select();
            $this->assign('user_list', json_encode($list, 256));
            $this->assign('id', '');
            return $this->fetch();
        }
    }
}


